package com.woniu.mq;
import com.rabbitmq.client.Channel;
import com.woniu.dao.PreOrderMapper;
import com.woniu.pojo.PreOrder;
import com.woniu.util.DateUtil;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;


import java.io.IOException;
import java.text.ParseException;
import java.util.Date;
import java.util.Map;
@Component
public class OrderConsumer {
    @Autowired
    private PreOrderMapper preOrderMapper;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @RabbitListener(queues = {"horder-queue"})
    @RabbitHandler
    public void order(@Payload Map map, Channel channel, @Headers Map headers) {
        System.out.println("zzzzzzzzzzzzzzzzzzzzzzzzzzz");
        System.out.println(map.get("createtime"));
        Date date=DateUtil.getDate((Long) map.get("createtime"));
        System.out.println("******************");
        System.out.println(date);
        System.out.println("******************");


        PreOrder preOrder = new PreOrder();
        preOrder.setCash((Integer) map.get("cash"));
        preOrder.setCreatetime(DateUtil.getDate((Long) map.get("createtime")));
//        preOrder.setCreatetime((Date) map.get("createtime"));
        preOrder.setUid((Integer) map.get("uid"));
        preOrder.setPuId((Integer) map.get("puId"));
        preOrder.setMuId((Integer) map.get("muId"));
        preOrder.setName((String) map.get("name"));
        preOrder.setLocation((String) map.get("location"));
        preOrder.setNum((String) map.get("num"));
//        preOrder.setStarttime((Date) map.get("starttime"));
//        preOrder.setEndtime((Date) map.get("endtime"));
        preOrder.setStarttime(DateUtil.getDate((Long) map.get("starttime")));
        preOrder.setEndtime(DateUtil.getDate((Long) map.get("endtime")));
        preOrder.setMoney((Integer) map.get("money"));
        preOrder.setMethod((String) map.get("method"));
        preOrder.setStatus((String) map.get("status"));
        preOrder.setMark((Integer) map.get("mark"));
        preOrderMapper.insertSelective(preOrder);
        Long id = (Long) headers.get(AmqpHeaders.DELIVERY_TAG);
        try {
            channel.basicAck(id, false);
        } catch (IOException e) {
            e.printStackTrace();
        }
        rabbitTemplate.convertAndSend("hpay-exchange","pay",map);
    }
}
